package ast;

import java.util.ArrayList;

import compiler.OutputFormatter;
import compiler.Utils;

public class StatementList {
	private ArrayList<Statement> list;

	public StatementList(ArrayList<Statement> list) {
		this.list = list;
	}

	/**
	 * Verifies if this list has a return statement and verifies if the type returned and the given type matches
	 * @param t The type to be tested
	 * @return true if this statement list correctly returns an expression of the given type, false otherwise
	 */
	public boolean hasReturnStatement(Type t) {
		boolean ret = false;
		for (Statement s : list)
			if (s instanceof ReturnStatement) {
				ret = !Utils.checkExpr(((ReturnStatement) s).getExpression(), t);
			}
			else if (s instanceof WhileStatement)
				ret = ret || ((WhileStatement) s).getWhileStatement().hasReturnStatement(t);
			else if (s instanceof IfStatement) {
				boolean ifReturns = ((IfStatement) s).getIfStatement().hasReturnStatement(t);
				boolean elseReturns = (((IfStatement) s).getElseStatement() != null && ((IfStatement) s).getElseStatement().hasReturnStatement(t));
				ret = ret || (ifReturns && elseReturns);
			}
		return ret;
	}

	/**
	 * Generates the C source code of the current knot
	 * 
	 * @param of
	 *            The OutputFormatter where the code will be generated
	 */
	public void genC(OutputFormatter of) {
		for (Statement statement : list)
			statement.genC(of);
	}
}
